﻿#ifndef _EVENTSERVER_H
#define _EVENTSERVER_H

#include "server.h"
#include "libevent/event.h"

namespace SocketLib
{
    class BaseEventServer;

    class EventSocket : public BaseSocket
    {
        public:
        EventSocket();
        void*       getEventServer();

        private:
        friend class BaseEventServer;

        struct  event   ev_read;
        void*           m_peventserver;
    };

    class BaseEventServer : public BaseServer
    {
        protected:
        event&  _getEventSocketReadEvent(EventSocket&);
        void    _setEventServer(EventSocket&, void*);
        void*   _getEventServer(EventSocket&);
    };

    template<typename SOCKETSTRUCT, int NUM>
    class EventServer : public BaseServer
    {
        public:

        EventServer();
        bool    listen(int aPort, int aBackNum = 5);
        bool    poll(long aOverTime);
        void    disConnection(SOCKETSTRUCT*);

        private:
        void    _handlerNewClient();
        bool    _handlerClientRecv(SOCKETSTRUCT*);
        void    _disConnection(SOCKETSTRUCT*);

        private:

        TypePoll<false, SOCKETSTRUCT, NUM> m_pool;

        sock            m_listenSock;
        struct  event   m_listen_ev_read;

        bool            m_bRun;

        private:

        static  void    S_OnAccept(int, short, void* arg);
        static  void    S_OnReadData(int, short, void* arg);
    };
}

#include "../src/eventserver.cpp"

#endif
